package com.ciwong.epaper.modules.dictation.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.guidedialog.CountDownDialog;
import com.ciwong.epaper.modules.dictation.bean.DictationAnswerInfo;
import com.ciwong.epaper.modules.epaper.bean.Answer;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.modules.epaper.util.RepeatKeyUtil;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.me.bean.WorkContents;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DoubleClickCheckUtils;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.ESystem;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.libs.utils.FileUtils;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.ciwong.mobilelib.widget.CWDialog;
import com.google.gson.reflect.TypeToken;
import com.loco.handsomemo.lib_keyboard.KeyBoardWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class BaseDictationActivity extends BaseActivity implements View.OnClickListener, CountDownDialog.CountInterFace {

    public ArrayList<WordDetail> words;
    public boolean isRepeatWord;//是否来自于单词重读
    protected DownLoadInfo mDownLoadInfo;

    protected Module mModule;

    protected int position;

    // 是否显示中文
    public boolean mIsShowEnglishMode = false;

    protected CopyOnWriteArrayList<DictationAnswerInfo> answerRecorders;
    protected DictationAnswerInfo reListenWorkAnswer;
    /**
     * 正常状态
     */
    public static final int OPERATE_STATUS_NORMAL = 0;
    /**
     * 暂停状态
     */
    public static final int OPERATE_STATUS_STOP = 1;


    public int operateStatus = OPERATE_STATUS_NORMAL;// 操作状态

    public int currentListenAndRepeatIndex, cacheListenAndRepeatIndex;

    protected String intentWorkId = "0";

    protected String mUuid = UUID.randomUUID().toString();

    protected String jsonResourcePath;

    protected String checkedResource;
    protected String mResourceName, mClassId;
    protected int workLong = 0;// 总用时

    protected long startDate = System.currentTimeMillis();// (开始做作业时间)(毫秒)

    protected boolean isFirstPlay;
    protected CountDownDialog countDownDialog;
    protected Handler mHandler = new Handler();

    public long startTime;
    protected EditText mEtAnswer;
    private TextView mTvCurrentIndex;
    private TextView mTvWordCounts, mTvComplete;
    private ImageView mIvNextWord;
    private ImageView mIvPreWord;
    private ImageView mIvRePlay;
    private ImageView mIvPlayLs;
    private LinearLayout rootView;
    private TextView tv_word_pretations;
    private RelativeLayout mRlRecordTips;
    private ImageView iv_english_china;
    private int mServiceId;
    private String mVersionId;
    private int moduleId;
    protected WorkContents mWorkContents;
    protected long workStartTime;

    protected Timer timer;
    protected boolean isTimering;// 是否正在计时

    public static final String HAS_COMPLETED_WORK = "HAS_COMPLETED_WORK";
    /**
     * 截止时间
     */
    public long effectiveDate = -1;


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_word_listen_play) {
            if (DoubleClickCheckUtils.vertifyDuration()) {
                playRepeat();
            }

        } else if (id == R.id.iv_word_listen_next || id == R.id.tv_complete) {
            if (DoubleClickCheckUtils.vertifyDuration()) {
                playNext();
            }

        } else if (id == R.id.iv_word_listen_pre) {
            if (DoubleClickCheckUtils.vertifyDuration()) {
                if (currentListenAndRepeatIndex == 0) {
                    ToastUtil.INSTANCE.toastCenter(BaseDictationActivity.this, "已经是第一个单词了");
                } else {
                    playForword();
                }

            }

        } else if (id == R.id.iv_english_china) {
            if (DoubleClickCheckUtils.vertifyDuration()) {
                if (!mIsShowEnglishMode) {
                    iv_english_china.setSelected(true);
                } else {
                    iv_english_china.setSelected(false);
                }
                mIsShowEnglishMode = !mIsShowEnglishMode;
                try {
                    CWSys.setSharedBoolean(EConstants.SHARE_KEY_IS_OPEN_DICATATION_ENGILSH_MODE, mIsShowEnglishMode);
                } catch (Exception e) {
                }
                refreshWordUi(currentListenAndRepeatIndex);
            }
        }
    }


    @Override
    protected int setView() {
        return R.layout.activity_dictation;
    }

    @Override
    protected void findViews() {
        rootView = findViewById(R.id.root_view);
        mEtAnswer = findViewById(R.id.tv_word_listen_answer);
        mTvCurrentIndex = findViewById(R.id.tv_word_listen_curent_index);
        mTvWordCounts = findViewById(R.id.tv_word_listen_count);
        mIvNextWord = findViewById(R.id.iv_word_listen_next);
        mIvPreWord = findViewById(R.id.iv_word_listen_pre);
        mIvRePlay = findViewById(R.id.iv_word_listen_play);
        mIvPlayLs = findViewById(R.id.iv_word_listen_play_ls);
        mRlRecordTips = findViewById(R.id.student_jilu);
        mTvComplete = findViewById(R.id.tv_complete);
        tv_word_pretations = findViewById(R.id.tv_word_pretations);
        iv_english_china = findViewById(R.id.iv_english_china);
    }

    @Override
    protected void initEvent() {
        mIvNextWord.setOnClickListener(this);
        mIvPreWord.setOnClickListener(this);
        mIvRePlay.setOnClickListener(this);
        mTvComplete.setOnClickListener(this);
        iv_english_china.setOnClickListener(this);
    }

    @Override
    protected void init() {
        KeyBoardWrapper.INSTANCE.initKeyBoard(this, rootView, mEtAnswer);
        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                BaseDictationActivity.this.goBack();
            }
        });

        mIsShowEnglishMode = CWSys.getSharedBoolean(
                EConstants.SHARE_KEY_IS_OPEN_DICATATION_ENGILSH_MODE,
                false);
        iv_english_china.setSelected(mIsShowEnglishMode);
    }


    public void goBack() {
        //
        try {
            if (isRepeatWord) {//
//				compelteRedoDictation();
                finish();
            } else {
                showDailog();
            }
        } catch (Exception e) {

        }

    }

    private void showDailog() {
        if (operateStatus != OPERATE_STATUS_STOP) {
            pause();
        }
        CWDialog mDialog = new CWDialog(this);
        // mDialog.setTitle(R.string.lear_title);
        mDialog.setMessage(R.string.confirm_back_work);
        mDialog.setPositiveButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
        mDialog.setNegativeButton(R.string.ok,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showCricleProgress("正在保存记录！");
                        //saveAnswer();
                        CWSys.setSharedInt(getSaveTime(), workLong);
                        finish();
                    }
                });
        // }
        mDialog.show();
    }

    @Override
    protected void loadData() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        Intent intent = getIntent();
        if (intent != null) {
            isRepeatWord = intent.getBooleanExtra(IntentFlag.INTENT_FLAG_ID, false);
            if (isRepeatWord) {//来自于重新听写某个单词
                words = new ArrayList<WordDetail>();
                WordDetail wordDetail = (WordDetail) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
                words.add(wordDetail);
                mDownLoadInfo = (DownLoadInfo) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
                reListenWorkAnswer = (DictationAnswerInfo) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_ANSWER);
                mResourceName = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
                effectiveDate = getIntent().getLongExtra(IntentFlag.INTENT_FLAG_WORK_EFFECTIV_TIME, -1);
                currentListenAndRepeatIndex = 0;
                refreshWordUi(0);
            } else {
                mDownLoadInfo = (DownLoadInfo) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
                mModule = (Module) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
                position = intent.getIntExtra(IntentFlag.INTENT_FLAG_POSITION, 0);

                String workId = intent.getStringExtra(IntentFlag.INTENT_FLAG_WORK_ID);
                checkedResource = intent.getStringExtra(IntentFlag.INTENT_FLAG_CHECKED_RESOURCE);

                mClassId = intent.getStringExtra(IntentFlag.INTENT_FLAG_CLASS_ID);
                mVersionId = intent.getStringExtra(IntentFlag.INTENT_FLAG_WORK_CONTENT);
                mServiceId = getIntent().getIntExtra(IntentFlag.INTENT_FLAG_SERVICE_ID, -1);
                effectiveDate = getIntent().getLongExtra(IntentFlag.INTENT_FLAG_WORK_EFFECTIV_TIME, -1);
                if (mDownLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {

                    jsonResourcePath = ESystem.getPackagesJsonPathNew(mDownLoadInfo);
                    moduleId = Integer.valueOf(mDownLoadInfo.getModuleId());

                    if (null == mVersionId) {
                        mVersionId = mDownLoadInfo.getVersionId();
                    }
                    mResourceName = mDownLoadInfo.getResourceName();

                } else {
                    jsonResourcePath = ESystem.getPackagesJsonPath(mModule.getResourceList().get(position).getResourceFile());
                    ModuleContent moduleContent = mModule.getResourceList().get(position);
                    moduleId = mModule.getModuleInfo().getModuleId();

                    if (null == mVersionId) {
                        mVersionId = moduleContent.getVersionId();
                    }
                    mResourceName = mModule.getResourceList().get(position).getResourceName();

                }

                mWorkContents = (WorkContents) getIntent().getSerializableExtra(IntentFlag.INTENT_FLAG_WORK_CONTENTS);
                if (!TextUtils.isEmpty(workId)) {
                    intentWorkId = workId;
                }
            }

        }
        //获取本地作业开始时间
        workStartTime = CWSys.getSharedLong(ListenSpeakUtil.getAnswerStartTimeKey(mDownLoadInfo, mModule, position, intentWorkId), 0);
        if (workStartTime == 0) {
            //本地没有 则使用传过来的时间并保存本地
            if (null != mDownLoadInfo) {
                workStartTime = mDownLoadInfo.getStartTime();
                CWSys.setSharedLong(ListenSpeakUtil.getAnswerStartTimeKey(mDownLoadInfo, mModule, position, intentWorkId), workStartTime);
            }
        }

        if (isRepeatWord) {
            mTvComplete.setVisibility(View.VISIBLE);
            mIvNextWord.setVisibility(View.GONE);
            mIvPreWord.setVisibility(View.GONE);
        } else {
            mIvNextWord.setVisibility(View.VISIBLE);
            mIvPreWord.setVisibility(View.VISIBLE);
            mTvComplete.setVisibility(View.GONE);
        }

        if (isRepeatWord) {
            startRedoWork();
        } else {
            String uuidkey = RepeatKeyUtil.getWordUuidSaveKey(mDownLoadInfo, mModule, position, intentWorkId, 0);
            final String saveUUid = CWSys.getSharedString(uuidkey, null);
            final long startTime = CWSys.getSharedLong(getStartTime(), System.currentTimeMillis());
            if (null != saveUUid) {
                mUuid = saveUUid;
                workLong = (int) (System.currentTimeMillis() - startTime) / 1000;
                int oldWorkLong = CWSys.getSharedInt(getSaveTime(), 0);
                if (workLong < oldWorkLong || workLong < 0) {
                    workLong = oldWorkLong;
                }
            } else {
                CWSys.setSharedString(uuidkey, mUuid);
                CWSys.setSharedLong(getStartTime(), startDate);
            }
            ///
            if (EConstants.IS_YOUKE && EConstants.WORD_IS_YOUKE_LOGIN) {
                EConstants.WORD_IS_YOUKE_LOGIN = false;
                FileUtils.delete(ESystem.getAnswersUuidPath(saveUUid));
                clearRecord();
                currentListenAndRepeatIndex = 0;
                loadWordDetails(mUuid, 0);
            } else {
                EConstants.WORD_IS_YOUKE_LOGIN = false;
                getSaveUserAnswerRecord(mUuid);
            }
        }


        setTitleText(mResourceName);

    }

    //重做页面回来开始
    private void startRedoWork() {
        startTimer();
        currentListenAndRepeatIndex = 0;
        play(false);
    }

    public abstract void playNext();//下一题

    public abstract void playForword();//上一个

    public abstract void playRepeat();//重播

    /**
     * @param isPlayRepeat 是否是重播当前单词
     */
    public abstract void play(boolean isPlayRepeat);//开始

    public abstract void pause();//暂停

    public abstract void saveAnswer();

    public abstract boolean pigai(String wordAnswer);

    @Override
    public void close() {

    }

    public void clearRecord() {//清楚记录

    }

    /**
     * 获取单词
     */
    public void loadWordDetails(final String saveUUid, final int currPoint) {
        EpaperDao.getInstance().getSerializableObjects(jsonResourcePath, new TypeToken<List<WordDetail>>() {
                }.getType(), new BaseExtCallBack(BaseDictationActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {

                    @Override
                    public void success(final Object data) {
                        super.success(data);
                        ArrayList<WordDetail> tempWords = (ArrayList<WordDetail>) data;
                        if (!TextUtils.isEmpty(checkedResource)) {
                            // 有选题
                            String[] checkedWords = checkedResource.split(",");
                            words = new ArrayList<WordDetail>();
                            for (String checkedWordId : checkedWords) {
                                WordDetail wd = new WordDetail();
                                wd.setwId(checkedWordId);
                                int index = tempWords.indexOf(wd);
                                if (index != -1) {
                                    words.add(tempWords.get(index));
                                }
                            }
                        } else {
                            words = tempWords;
                        }
                        if (words != null && !words.isEmpty()) {

                            // workLong = CWSys.getSharedInt(getSaveTime(), 0);
                            refreshWordUi(currPoint);
                            if (currPoint < words.size()) {
                                //自动播放
                                autoPlay(currPoint);
                            } else {
                                allComplete();
                            }

                        }
                        hideCricleProgress();
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        super.failed(errorCode, data);
                        hideCricleProgress();
                    }

                    @Override
                    public void failed(Object data) {
                        finish();
                        showToastError((int) data);
                        // hideMiddleProgressBar();
                        hideCricleProgress();
                    }
                }

        );
    }

    /**
     * 设置中文翻译
     *
     * @param currPoint
     */
    protected void refreshWordUi(int currPoint) {
        if (iv_english_china.isSelected()) {
            tv_word_pretations.setVisibility(View.VISIBLE);
            if (currPoint >= 0 && currPoint < words.size()) {
                WordDetail mWordDetail = words.get(currPoint);
                tv_word_pretations.setText(mWordDetail.getPretations());
            }
        } else {
            tv_word_pretations.setVisibility(View.GONE);
        }
    }

    /**
     * 开始计时
     */
    protected void startTimer() {
        try {
            cancelTimer();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    workLong++;

                }
            }, 0, 1000);
            isTimering = true;
        } catch (Exception e) {
            e.getStackTrace();
        }

    }

    /**
     * 取消计时
     */
    protected void cancelTimer() {
        if (timer != null && isTimering) {
            timer.cancel();
            isTimering = false;
        }
    }

    //倒计时3秒自动开始
    public void autoPlay(final int currPoint) {

        countDownDialog = new CountDownDialog(BaseDictationActivity.this, this);
        countDownDialog.setCancelable(false);
        countDownDialog.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (countDownDialog != null && countDownDialog.isShowing()) {
                    countDownDialog.dismiss();
                    showTopTip(currPoint);
                }
            }
        }, 3500);
    }

    /**
     * 获取作业用时key
     *
     * @return
     */
    protected String getSaveTime() {
        String key = ESystem.getSaveRecordKeywithMode(mDownLoadInfo, mModule, position, intentWorkId, SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_TIME, 0);
        return key + "_" + getUserInfoBase().getUserId();
    }

    /**
     * 获取作业开始时间key
     *
     * @return
     */
    protected String getStartTime() {
        String key = ESystem.getSaveRecordKeywithMode(mDownLoadInfo, mModule, position, intentWorkId, SerializableManager.SerializeKey.SHARE_KEY_DO_WORK_START_TIME, 0);
        return key + "_" + getUserInfoBase().getUserId();
    }


    //设置做题进度
    public void setNo(int index, int total) {
        mTvCurrentIndex.setText("" + index);
        mTvWordCounts.setText("/" + total);
    }


    /**
     * 获取保存的用户答案记录
     */
    private void getSaveUserAnswerRecord(final String saveUUid) {

        Log.d(TAG, "#######getSaveUserAnswerRecord  saveUUid#########" + saveUUid);

        getSaveUserAnswerList(new BaseExtCallBack(BaseDictationActivity.this) {
            @Override
            public void success(Object data) {
                //继续学习按钮回调
                try {
                    Log.d(TAG, "#######getSaveUserAnswerRecord  success#########" + mUuid);
                    CopyOnWriteArrayList<DictationAnswerInfo> saveAnswersList;
                    if (data instanceof CopyOnWriteArrayList) {
                        saveAnswersList = (CopyOnWriteArrayList<DictationAnswerInfo>) data;
                    } else {
                        saveAnswersList = new CopyOnWriteArrayList<>();
                        saveAnswersList.addAll((ArrayList<DictationAnswerInfo>) data);
                    }
                    int currPoint = 0;
                    if (saveAnswersList != null) {
                        answerRecorders = saveAnswersList;
                        currPoint = saveAnswersList.size();
                        DictationAnswerInfo listenWordAnswerInfo = saveAnswersList.get(currPoint - 1);
                        //最后一个答案为空时，还要播最后一个
                        if (TextUtils.isEmpty(listenWordAnswerInfo.getWordAnswer())) {
                            boolean hasCompletedWork = CWSys.getSharedBoolean(mUuid + HAS_COMPLETED_WORK, false);
                            if (!hasCompletedWork) {
                                currPoint--;
                            }

                        }
                        currentListenAndRepeatIndex = currPoint;
                    }
                    loadWordDetails(mUuid, currPoint);
                } catch (Exception e) {

                }

            }

            @Override
            public void failed(int errorCode, Object data) {
                failed(data);
            }

            @Override
            public void failed(Object data) {
                try {
                    Log.d(TAG, "#######getSaveUserAnswerRecord  failed#########" + mUuid);
                    answerRecorders = new CopyOnWriteArrayList<>();
                    FileUtils.delete(ESystem.getAnswersUuidPath(saveUUid));
                    clearRecord();
                    currentListenAndRepeatIndex = 0;
                    loadWordDetails(mUuid, 0);
                } catch (Exception e) {

                }

            }
        }, saveUUid);
    }


    /**
     * 获取保存的答案记录
     *
     * @param baseCallBack
     */
    protected void getSaveUserAnswerList(final BaseCallBack baseCallBack, String saveUUid) {


        SerializableManager.getInstance().deSerialize(RepeatKeyUtil.getWordRepeatAnswerListKey(mDownLoadInfo, mModule, position, intentWorkId, saveUUid),

                new BaseCallBack() {
                    @Override
                    public void success(Object data) {

                        try {
                            CopyOnWriteArrayList<DictationAnswerInfo> answerList;
                            if (data instanceof CopyOnWriteArrayList) {
                                answerList = (CopyOnWriteArrayList<DictationAnswerInfo>) data;
                            } else {
                                answerList = new CopyOnWriteArrayList<>();
                                answerList.addAll((ArrayList<DictationAnswerInfo>) data);
                            }

                            baseCallBack.success(answerList);

                        } catch (Exception e) {
                            e.getStackTrace();
                        }
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        Log.d(TAG, "#######反序列化失败#############");
                        baseCallBack.failed(errorCode, data);
                    }


                    @Override
                    public void failed(Object data) {

                        if (null != data) {
                            Log.d(TAG, "#######反序列化失败 getSaveUserAnswerList failed data.toString()#############" + data.toString());
                        } else {
                            Log.d(TAG, "#######反序列化失败 getSaveUserAnswerList failed#############");

                        }

                        baseCallBack.failed(data);
                    }
                });
    }

    /**
     * 设置播放图标的显示状态
     */
    protected void setPlayBtnState(boolean isAudioPlaying, boolean isFirstPlay) {

        if (isAudioPlaying) {
            mIvRePlay.setVisibility(View.INVISIBLE);
            mIvPlayLs.setVisibility(View.VISIBLE);
            Drawable drawable = mIvPlayLs.getBackground();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).start();
            }


        } else {
            Drawable drawable = mIvPlayLs.getBackground();
            if (drawable != null && drawable instanceof AnimationDrawable) {
                ((AnimationDrawable) drawable).stop();
            }
            mIvPlayLs.setBackgroundDrawable(null);
            mIvPlayLs.setBackgroundResource(R.drawable.play_animation_ls_word_listen);
            if (!isFirstPlay) {
                showRePlayImageView();
				/*ObjectAnimator animator = ObjectAnimator.ofFloat(mIvRePlay, "alpha", 0, 1);
				animator.setDuration(1000);
				animator.start();*/
            }

        }
    }

    protected void showRePlayImageView() {
        mIvPlayLs.setVisibility(View.INVISIBLE);
        mIvRePlay.setVisibility(View.VISIBLE);
    }

    public Answer getAnswer() {//获取提交答案信息
        Answer answer = new Answer();
        try {
            //组装提交的答案JSON
            answer.setWorkLong(workLong);
            //保存一下作业用时
            answer.setWorkId(intentWorkId);
            //服务id
            answer.setWorkType(mServiceId);
            if (mClassId == null && EApplication.getInstance().getClazz() != null) {
                //如果classid  没有传进来，用默认的,班级id是必须的，要不然老师端收不到
                mClassId = String.valueOf(EApplication.getInstance().getClazz().getId());
            }

            if (null != mClassId) {
                answer.setClassId(mClassId);
            }
            if (getUserInfoBase() != null)
                answer.setUserName(getUserInfoBase().getRealName());

            answer.setJsonVersion(EConstants.JSON_VERSION);
            answer.setUserAnswer(ESystem.USER_ANSWER);
            answer.setCreateTime(System.currentTimeMillis());
            answer.setBrandId(EApplication.BRAND_ID);


            answer.setVersionId(mVersionId);
            if (null != mDownLoadInfo) {

                if (mDownLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_OLD) {
                    ModuleContent moduleContent = mModule.getResourceList().get(position);
                    moduleId = mModule.getModuleInfo().getModuleId();
                    if (mWorkContents != null) {
                        answer.setResourceName(mWorkContents.getResourceName());
                    } else {
                        answer.setResourceName(moduleContent.getResourceName());
                    }
                    answer.setParentVersionId(moduleContent.getParentVersionId());
                    answer.setModuleId(ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION);
                    answer.setResourceType(moduleContent.getResourceType());
                } else {
                    answer.setResourceName(mDownLoadInfo.getResourceName());
                    answer.setParentVersionId("0");
                    answer.setModuleId(ModuleInfo.ModuleInfoId.MODULE_ID_DICTATION);
                    answer.setResourceType(mDownLoadInfo.getResourceType());
                }
            }


            if (mDownLoadInfo != null) {
                answer.setPackageId(mDownLoadInfo.getBookId());
                answer.setcId(mDownLoadInfo.getChapterId());
            }
            //暂时先传0  作业内容ID(来源于学生作业记录接口)
            if (mWorkContents == null) {
                answer.setContentId(0);
            } else {
                answer.setContentId(mWorkContents.getContentId());
            }
            answer.setWorkScore(100);//作业满分分值 暂时先传100
            return answer;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return answer;
    }

    public void showTopTip(final int currPoint) {
        if (currPoint > 0 && currPoint <= words.size()) {
            mRlRecordTips.setVisibility(View.VISIBLE);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mRlRecordTips.setVisibility(View.GONE);
                }
            }, 2000);
        }
    }

    @Override
    public void onBackPressed() {
        BaseDictationActivity.this.goBack();
    }

    public abstract void allComplete();

    protected void compelteRedoDictation() {
        String wordAnswer = mEtAnswer.getText().toString();
        reListenWorkAnswer.setWordAnswer(wordAnswer);
        reListenWorkAnswer.setRight(pigai(wordAnswer));
        if (currentListenAndRepeatIndex >= 0 && currentListenAndRepeatIndex < words.size()) {
            reListenWorkAnswer.setPretations(words.get(currentListenAndRepeatIndex).getPretations());
        }
        Intent intent = new Intent();
        intent.putExtra(IntentFlag.INTENT_FLAG_ANSWER, reListenWorkAnswer);
        intent.putExtra(IntentFlag.INTENT_FLAG_WORK_TIME, workLong);
        setResult(RESULT_OK, intent);
        finish();
    }
}
